//////////////////////////////////////////////////////////////////////////
// FileName:UserLoginLog
// CreateBy:DragonXiong
// CreateDate:2009-5-31
// Description:
//		
//      
// Modify Log:090624:Add User Defined GridView to Show Data
//	
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;
using Auto.Data.Setting;
using Auto.Web.Common;
using Auto.Data.Systems.BLL;

namespace BaseSystem
{
    public partial class UserLoginLog : CallBackHelper
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Loading();
                InitPage();
            }
        }
        private void Loading()
        {
            HttpContext hc = HttpContext.Current;
            hc.Response.Write("<div id='loading' style='position:absolute; top:50%; left:50%;'> ");
            hc.Response.Write("<img src='Images/Loading/loading.gif'/> ");
            hc.Response.Write("</div>");
            //hc.Response.Write("<script>mydiv.innerText = '';</script>");
            hc.Response.Write("<script type=text/javascript>");
            hc.Response.Write("function document.onreadystatechange()");
            hc.Response.Write(@"{ try  
                                   {
                                    if (document.readyState == 'complete') 
                                    {
                                         
                                         delNode('loading');
                                        
                                    }
                                   }
                                 catch(e)
                                    {
                                        alert('Page Load Failed!');
                                    }
                                                        } 

                            function delNode(nodeId)
                            {   
                                try
                                {   
                                      var div =document.getElementById(nodeId); 
                                      if(div !==null)
                                      {
                                          div.parentNode.removeChild(div);   
                                          
                                          div=null; 
                                          
                                          CollectGarbage(); 
                                      } 
                                }
                                catch(e)
                                {   
                                   alert('Delete ID: '+nodeId+' Error!');
                                }   
                            }

                            ");

            hc.Response.Write("</script>");
            hc.Response.Flush();
        }

        #region Init Page
        private void InitPage()
        {
            this.GridView1.Attributes.Add("SortExpression", "LoginTime");
            this.GridView1.Attributes.Add("SortDirection", "ASC");

            Count();

            BindGridView();
        }
        private void Count()
        {
            PagerList pagerList = new PagerList();
            this.GridView1.Attributes.Add("UserLoginLogCount", pagerList.Count("_UsersLoginLog", "").ToString());
        }
        #endregion Init Page

        #region Data Bind


        protected void BindGridView()
        {
            int currentPageIndex = Convert.ToInt32(this.GridView1.Attributes["UserLoginLogCount"]) - (currentGlobalPageIndex - 1) * this.GridView1.PageSize;

            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];

            
            PagerList pagerList = new PagerList();
            DataTable dt = pagerList.GetList("_UsersLoginLog", "RecID", "RecID,LoginTime,LogoutTime,UserPCIP,UserPCName,LogoutType", "", this.GridView1.PageSize, currentPageIndex == 0 ? currentPageIndex + 1 : currentPageIndex, sortExpression, sortDirection);

            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();

            GridViewBootom = true;
        }

        #endregion Data Bind

        #region Event
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            base.PagerTemplate(this.GridView1, GetCount(), "divPage");
          
        }
        #endregion Event

        private bool GridViewBootom
        {
            set {
                GridViewRow pagerRow1 = GridView1.BottomPagerRow;
                if (pagerRow1 != null)
                {
                    pagerRow1.Visible = value;
                }
            }
        }
        private int GetCount()
        {
            return Convert.ToInt32(this.GridView1.Attributes["UserLoginLogCount"]);
        }
        #region ICallbackEventHandler

        public override string GetCallbackResult()
        {
            GridViewBootom = true;
            return RenderControl(this.GridView1);
        }

        public override void RaiseCallbackEvent(string eventArgument)
        {
            string[] col = eventArgument.Split('|');
            if (col[0] == "Page")
            {
                currentGlobalPageIndex = Convert.ToInt32(col[3]); 
                pager = col[4];
            }
            if (!string.IsNullOrEmpty(col[1]))
            {
                this.GridView1.Attributes["SortExpression"] = col[1];
            }
            this.GridView1.Attributes["SortDirection"] = col[2];
            BindGridView();
        }

        #endregion ICallbackEventHandler
        
    }
}
